package com.sckj.springboot.service.impl;

import com.sckj.springboot.entity.User;
import com.sckj.springboot.po.UserMapper;
import com.sckj.springboot.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Transactional
    public void insertUser(User user) {
        //插入数据
        userMapper.insertUser(user);
        //手动抛出异常
        throw new RuntimeException();
    }

    @Transactional
    public void insertUser2(User user) throws Exception {
       userMapper.insertUser(user);
       throw new SQLException("数据库异常");
    }

    @Transactional
    public void insertUser3(User user) {
        try {
            userMapper.insertUser(user);
            throw new SQLException("数据库异常");
        }catch (Exception ex){

        }
    }

    @Transactional(rollbackFor = Exception.class)
    public synchronized void insertUser4(User user) {
        userMapper.insertUser(user);
    }


}
